/*
*生成后台动态路由
* */
import {baseUrlAdmin, baseUrlAdminHome, baseUrlAdminLogin} from "./config";
import {interopDefault} from "../../.nuxt/utils";
import {getAction} from "../../api/api";

let  routeGenerator=  async (routers) => {
  //登录路由
  routers.push(
    {
      path: baseUrlAdminLogin,
      component: () => interopDefault(import('@/pages/api_server/common/login.vue')),
    },
  )
  //入口路由
  let main=    {
    path: baseUrlAdmin,
    component: () => interopDefault(import('@/pages/api_server/common/main.vue')),
    redirect: {path: baseUrlAdminHome},
    children: [],
  }
  //嵌套的所有子路由
  const {result} =  await getAction("/api/sys/menu/tree")
  putAll(result,main.children);
  function putAll(source,target){
    source.forEach(item=>{
      if(item.children){
        putAll(item.children,target);
      }else if(item.path&&item.component){
        target.push(
          {
            path: item.path,
            component: () => interopDefault(import('@/pages/api_server/'+item.component+'.vue')),
          }
        )
      }
    })
  }
  routers.push(main)
  return routers;
}
export  {
  routeGenerator
}
